package com.atguigu.gmall.fastdfs;

import io.netty.util.concurrent.CompleteFuture;
import org.junit.Test;

import javax.swing.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lx
 * @Time: 10:22
 * @Description:
 */
public class CompletableFutureDemo {

    //创建能返回或不能回返值的线程
    @Test
    public void test1() throws Exception{
        //1:创建线程
   /*     CompletableFuture<String> javaCompletableFuture =
                CompletableFuture.completedFuture("java");
        System.out.println(javaCompletableFuture.get());*/
        //2:创建打印线程名称的线程
/*        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        });*/
        //System.out.println(voidCompletableFuture.get());
/*        CompletableFuture<String> supplyAsyncCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "supplyAsync";
        });
        System.out.println(supplyAsyncCompletableFuture.get());*/



    }
    //串行   任务1 -串行-》 任务2   任务2需要任务1的返回值
    @Test
    public void test2() throws Exception{
        System.out.println(Thread.currentThread().getName());
        //1:创建线程
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            int i = 1 + 3;
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int j = 1/0;
            return i;
        });
        //2:线程
        CompletableFuture<Void> voidCompletableFuture = completableFuture.thenAcceptAsync((t) -> {
            System.out.println("t是i吗：" + t);
        });
        //thenAccept() //表示：任务2直接使用任务1的线程
               //thenAccept Async () 表示： 任务2 去线程池获取一个新的线程来执行
        CompletableFuture<Integer> exceptionally = completableFuture.exceptionally((e) -> {
            System.out.println(e);
            return 1;
        });
        completableFuture.whenCompleteAsync((t,e) -> {
            System.out.println(t);
            System.out.println(e);
        });
        //等一会儿
        CompletableFuture.allOf(voidCompletableFuture,completableFuture,exceptionally).join();
    }

    //并行
    @Test
    public void test3() throws Exception{
        //1:
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "AAAAAA");
            return "haha";
        });
        //2:
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+ "BBBBBB");
        });
        System.out.println(Thread.currentThread().getName() + "test3");
        stringCompletableFuture.join();

        voidCompletableFuture.join();
    }
}
